-module(pb_role).

-export([pack/2, pack_msg/2, unpack/2, unpack_msg/2]).


%% 角色
pack(3900, {Rid,Name,Sex,Lev}) ->
	Bin1 = pb:encode(u64, Rid),
	Bin2 = pb:encode(string, Name),
	Bin3 = pb:encode(u8, Sex),
	Bin4 = pb:encode(u16, Lev),
	BinData = <<Bin1/binary,Bin2/binary,Bin3/binary,Bin4/binary>>,
	{ok, pb:msg(3900, BinData)};

%% 登录成功
pack(3000, {Rid}) ->
	Bin1 = pb:encode(u64, Rid),
	BinData = <<Bin1/binary>>,
	{ok, pb:msg(3000, BinData)};

pack(_Cmd, _Data) -> 
	{error, {unknown_command, _Cmd, _Data}}.


%% 角色
pack_msg(3900, {Rid,Name,Sex,Lev}) ->
	Bin1 = pb:encode(u64, Rid),
	Bin2 = pb:encode(string, Name),
	Bin3 = pb:encode(u8, Sex),
	Bin4 = pb:encode(u16, Lev),
	<<Bin1/binary,Bin2/binary,Bin3/binary,Bin4/binary>>;

%% 登录成功
pack_msg(3000, {Rid}) ->
	Bin1 = pb:encode(u64, Rid),
	<<Bin1/binary>>;

pack_msg(_Cmd, _Data) -> 
	{error, {unknown_command, _Cmd, _Data}}.


%% 角色
unpack(3900, _Bin0) ->
	{Rid, _Bin1} = pb:decode(u64, _Bin0),
	{Name, _Bin2} = pb:decode(string, _Bin1),
	{Sex, _Bin3} = pb:decode(u8, _Bin2),
	{Lev, _Bin4} = pb:decode(u16, _Bin3),
	{ok, {Rid,Name,Sex,Lev}};

%% 登录成功
unpack(3000, _Bin0) ->
	{Rid, _Bin1} = pb:decode(u64, _Bin0),
	{ok, {Rid}};

unpack(_Cmd, _Bin) -> 
	{error, {unknown_command, _Cmd, _Bin}}.

%% 角色
unpack_msg(3900, _Bin0) ->
	{Rid, _Bin1} = pb:decode(u64, _Bin0),
	{Name, _Bin2} = pb:decode(string, _Bin1),
	{Sex, _Bin3} = pb:decode(u8, _Bin2),
	{Lev, _Bin4} = pb:decode(u16, _Bin3),
	{{Rid,Name,Sex,Lev},_Bin4};

%% 登录成功
unpack_msg(3000, _Bin0) ->
	{Rid, _Bin1} = pb:decode(u64, _Bin0),
	{{Rid},_Bin1};

unpack_msg(_Cmd, _Bin) -> 
	{error, {unknown_command, _Cmd, _Bin}}.
